package lili.com.my.fragment;


import android.app.Fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;


import lili.com.my.R;
import lili.com.my.activity.CourseDetailActivity;
import lili.com.my.activity.CourseModel;
import lili.com.my.activity.LoginActivity;
import lili.com.my.activity.LoginModel;

/**
 * A simple {@link Fragment} subclass.
 */
public class CourseFragment extends Fragment {


    RecyclerView recyclerView;
    ViewPager viewPager;
    private ArrayList<CourseModel.Course> courselist=new ArrayList<>();
    private ArrayList<ImageView> mViewPagerImgs=new ArrayList<>();
    int curentposition;
   Handler handler=new Handler(new Handler.Callback() {
       @Override
       public boolean handleMessage(Message message) {  //接收消息
           handler.sendEmptyMessageDelayed(1,2000);  //再次发送消息   然后handleMessage（）接收，循环往复。
           curentposition++;
           viewPager.setCurrentItem(curentposition % mViewPagerImgs.size());
           return false;
       }
   });
    public CourseFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_course, container, false);
        recyclerView = view.findViewById(R.id.recyclerView);
        viewPager=view.findViewById(R.id.viewPager);
        loading();
        return view;
    }
    public void loading(){
        RequestParams params = new RequestParams("http://1633.applinzi.com/public/?s=App.Course.Getcourse");
        x.http().get(params, new Callback.CommonCallback<String>() {


             ImageView imageView;

            public void onSuccess(final String result) {
                Gson gson = new Gson();
                CourseModel courseModel = gson.fromJson(result, CourseModel.class);
                if (courseModel != null && courseModel.data != null && courseModel.data.courselist != null) {
                    courselist = courseModel.data.courselist;
                }
                GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
                recyclerView.setLayoutManager(gridLayoutManager);
                recyclerView.setAdapter(new MyAdatper());
                ArrayList<String> banner = courseModel.data.banner;

                ImageView imageView1=new ImageView(getActivity());
                x.image().bind(imageView1,banner.get(banner.size()-1)); //多添加一个最后的数据到第一条数据之前
                mViewPagerImgs.add(imageView1);

                for (int i = 0; i < banner.size(); i++) {
                    imageView = new ImageView(getActivity());
                    x.image().bind(imageView, banner.get(i));
                    mViewPagerImgs.add(imageView);
                }
                ImageView imageView2=new ImageView(getActivity());
                x.image().bind(imageView2,banner.get(0)); //多添加一个第一条数据到最后一条数据之后
                mViewPagerImgs.add(imageView2);

               viewPager.setAdapter(new ViewPagerAdatper());
                viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                    }

                    @Override
                    public void onPageSelected(int position) {
                         curentposition = position;

                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {
                         if (state!=ViewPager.SCROLL_STATE_IDLE){
                            return;
                        }
                       if (curentposition == 0){
                           viewPager.setCurrentItem(mViewPagerImgs.size()-2,false);
                       }else if (curentposition==mViewPagerImgs.size()-1){
                           viewPager.setCurrentItem(1,false);
                       }
                    }

                });
                viewPager.setCurrentItem(1);
                handler.sendEmptyMessageDelayed(1,2000); //先调用
                Log.i("CourseFragment", "onSuccess" + result);
            }

            public void onError(Throwable ex, boolean isOnCallback) {

            }
            public void onCancelled(CancelledException cex) {

            }

            public void onFinished() {

            }
        });
    }
    public class ViewPagerAdatper extends PagerAdapter{

        @Override
        public int getCount() {
            return mViewPagerImgs.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {

            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView=mViewPagerImgs.get(position);
            container.addView(imageView);
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mViewPagerImgs.get(position));
        }
    }
    public class MyAdatper extends RecyclerView.Adapter<MyViewHolder>{

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view=View.inflate(getActivity(),R.layout.item_course,null);
            MyViewHolder myViewHolder=new MyViewHolder(view);
            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            final CourseModel.Course course=courselist.get(position);
            holder.titleTv.setText(course.title);
            holder.contentTitleTv.setText(course.titlecontent);
            x.image().bind(holder.imageView,course.icon);
            holder.mrootView.setOnClickListener(new View.OnClickListener() {

                public void onClick(View view) {
                    Intent intent=new Intent(getActivity(), CourseDetailActivity.class);
                    intent.putExtra("imgurl",course.imgurl);
                    intent.putExtra("des",course.des);
                    intent.putExtra("videourl",course.videourl);
                    startActivity(intent);
                }
            });

        }

        @Override
        public int getItemCount() {
            return courselist.size();
        }
    }
    public class MyViewHolder extends RecyclerView.ViewHolder{

        private  ImageView imageView;
        private  TextView titleTv;
        private  TextView contentTitleTv;
        LinearLayout mrootView;

        public MyViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.img);
            titleTv = itemView.findViewById(R.id.title);
            contentTitleTv = itemView.findViewById(R.id.contenttitle);
            mrootView = itemView.findViewById(R.id.rootView);
        }
    }

}
